package com.spzx.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.DTO.SkuDTO;
import com.spzx.product.api.domain.VO.ItemVO;
import com.spzx.service.ProductService;
import com.spzx.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/product")
@Tag(name = "商品管理")
public class ProductController extends BaseController {
    @Resource
    ProductService productService;
    @Resource
    ProductSkuService productSkuService;

    @GetMapping("/list")
    @Operation(summary = "商品条件分页查询")
    public TableDataInfo listByCondition(Product product) {
        startPage();
        List<Product> products = productService.listByCondition(product);
        TableDataInfo tableDataInfo = new TableDataInfo(products, products.size());
        return tableDataInfo;
    }

    @PostMapping()
    @Operation(summary = "新增商品")
    public AjaxResult insertProduct(@RequestBody Product product) {
        Boolean aBoolean = productService.insertProduct(product);
        return toAjax(aBoolean);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商品详细信息")
    public AjaxResult getDetails(@PathVariable Long id) {
        return success(productService.getDetails(id));
    }

    @PutMapping
    @Operation(summary = "修改商品详细信息")
    public AjaxResult updateDetails(@RequestBody Product product) {
        return toAjax(productService.updateDetails(product));
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除商品信息")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        return toAjax(productService.deleteByIds(ids));
    }

    @GetMapping("/updateStatus/{id}/{status}")
    @Operation(summary = "更新上下架状态")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Short status) {
        return toAjax(productService.updateStatus(id, status));
    }

    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    @Operation(summary = "更新审核状态")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Short auditStatus) {
        return toAjax(productService.updateAuditStatus(id, auditStatus));
    }

    @InnerAuth
    @GetMapping("/hotInfo")
    @Operation(summary = "内部调用接口：获取首页热门商品信息")
    public R<List<ProductSku>> hotInfo() {
        return R.ok(productSkuService.listHotInfo());
    }

    @InnerAuth
    @GetMapping("/skuList")
    @Operation(summary = "内部调用接口：分页查询商品列表")
    public R<List<ProductSku>> skuList(SkuDTO skuDTO) {
        System.out.println(skuDTO.getCategory3Id());
        return R.ok(productSkuService.skuList(skuDTO));
    }

    @InnerAuth
    @GetMapping("/product/item/{skuId}")
    @Operation(summary = "内部接口:商品详情")
    ItemVO itemById(@PathVariable("skuId") Long id) {

        return productService.itemById(id);
    }


}
